package com.base.myproject.client.ui;

import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.jdbc.StoreInfo;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.form.StoreFilterField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;

public class BaseBusiLeftPanel<M extends ModelData> {

	protected ModelType type = null;
	protected List<ColumnConfig> configs;
	protected StoreInfo info = null;
	protected BaseStore<M> store = null;
	protected ContentPanel panel = null;
	protected BaseGrid<M> grid = null;

	boolean contentmenu;
	boolean contentkeynav;
	int mode=1;
	public BaseBusiLeftPanel(List<ColumnConfig> configs, ModelType type,
			StoreInfo info) {

		this(configs,type,info,true,true);
	}
	public BaseBusiLeftPanel(List<ColumnConfig> configs, ModelType type,
			StoreInfo info,boolean contentmenu,boolean contentkeynav) {
		this(configs,type,info,contentmenu,contentkeynav,1);
	}
	public BaseBusiLeftPanel(List<ColumnConfig> configs, ModelType type,
			StoreInfo info,boolean contentmenu,boolean contentkeynav,int mode) {

		this.contentmenu = contentmenu;
		this.contentkeynav = contentkeynav;
		this.type = type;
		this.configs = configs;
		this.info = info;
		this.mode= mode;
		init();
	}

	/**
	 * 设置动态默认值
	 */
	
	public void setdefault()
	{
		
	}
	@SuppressWarnings("unchecked")
	public void init() {

		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		
		//RowNumberer r = new RowNumberer();   //显示行号
		//configs.add(0,r);
		configs.add(0,cbs.getColumn()); //默认的选择方式，如果不加入就有bug，在外层不需要加入
		ColumnModel cm = new ColumnModel(configs);
		store = new BaseStore<M>(){
			public void add(M modeldata) {
				setdefault();
				add(modeldata, true, true);

			}
		};

		store.setType(type);
		store.setStoreinfo(info);
		store.setMode(mode);
		PagingToolBar toolBar = new PagingToolBar(50,true);
		// toolBar.bind( (BasePagingLoader)store.getLoader());
		store.setToolBar(toolBar);
		store.initcompleted();
//		toolBar.bind((BasePagingLoader)store.getLoader());
		//设置删除
		grid = new BaseGrid(store, cm,contentmenu,contentkeynav,null)
		{
			  public void deleterecord() {
				  if(candeleterecord())
				  super.deleterecord();
			  }
				public void doBeforeRowChange()
				{
					doBeforeRowChangeGrid();
				}
		};

		// grid.setSelectionModel(cbs);
		grid.setLoadMask(true);
		
		//注意上面的代码已经有了 0 行号，1 调勾 2是第一个字段，默认情况下是自动展开，如果不自动展开底层有bug
	//	grid.setAutoExpandColumn(configs.get(2).getId());
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
	//	grid.getView().setForceFit(true);
		BaseAdvSearch ba = new BaseAdvSearch();
		ba.bindBaseStore(store);

		panel = new ContentPanel();
		panel.setFrame(true);

		panel.setButtonAlign(HorizontalAlignment.CENTER);
		panel.setIconStyle("icon-active");
		panel.setHeading("");
//		panel.setLayout(new FlowLayout());
//
//		//panel.setSize(600, 350);
//		// grid.setSize(600, 350);
//		grid.setAutoHeight(true);
//		grid.setAutoWidth(true);
//		//grid.setHeight(SystemMessage.leftheight);
//		//grid.setWidth("100%");
//		//  DomHelper.insertHtml("beforeEnd", grid.el().dom, "sssssss");
//		
//		// grid.setWidth("100%");
//		// grid.setHeight("100%");
//		//panel.setScrollMode(Scroll.AUTO);
//		panel.add(ba.getAdvsearch(), new FlowData(1));
//		panel.add(grid, new FlowData(1));
//		
//
//		panel.setBottomComponent(toolBar);
//		//panel.setTopComponent(toolBar);
////		panel.add(toolBar,new FlowData(1));
//		panel.setScrollMode(Scroll.ALWAYS);
//		
//		//panel.setAutoHeight(true);
//		panel.setHeight(SystemMessage.managercenterheight);
//		//panel.setHeight("2000");
//		
//		panel.setWidth("100%");
//		
		//----------layout
	
		panel.setLayout(new TableLayout(1));
		
		TableData td1= new TableData();
		td1.setColspan(1);
		td1.setRowspan(1);
		//grid.setAutoHeight(true);
		//grid.setAutoWidth(true);
		
		panel.setBottomComponent(toolBar);
		//panel.setBottomComponent(new Button("---------------------------------------------"));
//		panel.setScrollMode(Scroll.ALWAYS);
//		panel.setHeight(SystemMessage.managercenterheight);
		//grid.setHeight("100%");

	    
		grid.setHeight("390");
		grid.setWidth("200");
//	    ContentPanel cp = new ContentPanel();  
//	    cp.setBodyBorder(false);  
//	 //   cp.setIcon(Examples.ICONS.table());  
//	    cp.setHeading("Basic Grid");  
//	    
//	    cp.setButtonAlign(HorizontalAlignment.CENTER);  
//	    cp.setLayout(new FitLayout());  
//	   // cp.setSize(600, 300);
//	   // cp.setAutoWidth(true);
////	    cp.setAutoHeight(true);
//	    cp.add(grid);
		//panel.setWidth("100%");
	   // panel.setAutoWidth(true);
		//暂时屏蔽，需要工作量太大，主要是处理码名映射部分需要工作
		//panel.add(ba.getAdvsearch(),td1);
		
		panel.add(grid, td1);
		panel.setHeight("480");
		panel.setWidth("220");
	
		//----------end layout
		//panel.setHeight("100%");
		StoreFilterField<M> field = new StoreFilterField<M>() {
			@Override
			protected boolean doSelect(Store<M> store,
					M parent, M record, String property,
					String filter) {
	/*			for (int i = 0; i < type.getFieldCount(); i++) {

					Object name = record.get(type.getField(i).getName());
					
					//TODO
					if (name!=null&&name.toString().indexOf(filter.toLowerCase()) != -1) {
						return true;
					}
				}
				return false;*/
			return	Select(store,parent,record,property,filter);
			}

			@Override
			protected void onFilter() {
				super.onFilter();

				// grid.getSelectionModel().select(0);
			}

		};
//		field.setWidth("100%");
		field.setAutoWidth(true);
		field.setToolTip("对当前页面进行查找");
		field.bind(store);
		field.setFieldLabel("当页搜索");
//		Button B = new Button("test");
//		B.addSelectionListener(new SelectionListener<ButtonEvent>(){
//
//			@Override
//			public void componentSelected(ButtonEvent ce) {
//				// TODO Auto-generated method stub
//				System.out.println(panel.getHeight());
//			}
//			
//		});
		panel.setTopComponent(field);
	}
	
	public void setWidth(int width) {
		panel.setWidth(width);
	}

	protected boolean Select(Store<M> store,
			M parent, M record, String property,
			String filter) {
		for (int i = 0; i < type.getFieldCount(); i++) {

			Object name = record.get(type.getField(i).getName());
			
			//TODO
			if (name!=null&&name.toString().indexOf(filter.toLowerCase()) != -1) {
				return true;
			}
		}
		return false;
	}
	/**
	 * 判断是否可以被删除，之类重载
	 * @return
	 */
	public boolean candeleterecord()
	{
		return true;
	}
	/**
	 * 换行前处理，之类可以重载
	 */
	public void doBeforeRowChangeGrid()
	{
	}
	public ContentPanel getPanel() {
		return panel;
	}

	public BaseGrid getGrid() {
		return grid;
	}

	public void setPanel(ContentPanel panel) {
		this.panel = panel;
	}

	public ModelType getType() {
		return type;
	}

	public void setType(ModelType type) {
		this.type = type;
	}

	public List<ColumnConfig> getConfigs() {
		return configs;
	}

	public void setConfigs(List<ColumnConfig> configs) {
		this.configs = configs;
	}

	public StoreInfo getInfo() {
		return info;
	}

	public void setInfo(StoreInfo info) {
		this.info = info;
	}

	public BaseStore<M> getStore() {
		return store;
	}

	public void setStore(BaseStore<M> store) {
		this.store = store;
	}

	/*
	 * public BasePagingLoader< PagingLoadConfig, PagingLoadResult<ModelData>>
	 * getBaseLoader() { Date date = new Date();
	 * System.out.println("客户端发送数据请求=="+date.getTime()/1000+"--");
	 * RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
	 * "BaseServlet?Action=Loaddata"
	 * +"&tableinftablename=ccode&tableinfsql=select cname from ccode");
	 * //System.out.println("storeinfo="+storeinfo.getStoreInfoString());
	 * builder.setHeader("Content-Type",
	 * "application/x-www-form-urlencoded;charset=utf-8");
	 * 
	 * builder.setCallback(new RequestCallback(){
	 * 
	 * @Override public void onError(Request request, Throwable exception) { //
	 * TODO Auto-generated method stub System.out.println("error....."); }
	 * 
	 * @Override public void onResponseReceived(Request request, Response
	 * response) { // System.out.println(response.getText().length()); Date date
	 * = new Date();
	 * System.out.println("store在callback 中获得到数据=="+date.getTime()/1000);
	 * 
	 * 
	 * }}); HttpProxy proxy = new HttpProxy(builder);
	 * 
	 * 
	 * 
	 * 
	 * // need a loader, proxy, and reader XmlReader reader = new
	 * XmlReader(type) {
	 * 
	 * @Override protected ListLoadResult newLoadResult(Object loadConfig, List
	 * models) { PagingLoadConfig pagingConfig = (PagingLoadConfig) loadConfig;
	 * PagingLoadResult <ModelData> result = new
	 * BasePagingLoadResult<ModelData>(models, pagingConfig.getOffset(),
	 * pagingConfig.getLimit());
	 * 
	 * // result.setOffset(2); return result; }
	 * 
	 * 
	 * };
	 * 
	 * 
	 * // BasePagingLoader<PagingLoadResult<ModelData>> loader = new
	 * BasePagingLoader<PagingLoadResult<ModelData>>(proxy,reader);
	 * 
	 * BasePagingLoader< PagingLoadConfig, PagingLoadResult<ModelData>> loader =
	 * new BasePagingLoader< PagingLoadConfig, PagingLoadResult<ModelData>>
	 * (proxy,reader); // BaseListLoader loader = new BaseListLoader(proxy,
	 * reader); Date date2 = new Date();
	 * System.out.println("客户端发送数据请求结束=="+date2.getTime()/1000);
	 * 
	 * // BaseStore<JDBCModelData> store = new BaseStore<JDBCModelData>( //
	 * loader); return loader; }
	 */
}
